package com.miao.controller;

import com.miao.dto.MediaFilesDto;
import com.miao.dto.MediaParamsDto;
import com.miao.service.IMediaFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class MediaFilesController {
    @Autowired
    private IMediaFilesService iMediaFilesService;

    @PostMapping("/upload/coursefile")
    public MediaFilesDto fuck(@RequestPart("filedata")MultipartFile multipartFile, @RequestParam(value = "objectname", required = false)String objectname) throws Exception {
        //获取机构id，还没实现，先瞎写一个
        Long comId = 1232141425L;
        //获取文件信息
        MediaParamsDto mediaParamsDto = new MediaParamsDto();
        mediaParamsDto.setFilename(multipartFile.getOriginalFilename());
        mediaParamsDto.setFileSize(multipartFile.getSize());
        mediaParamsDto.setFileType("001001");

        //获取本地路径
        //创建临时文件
        File minio = File.createTempFile("minio", ".temp");
        multipartFile.transferTo(minio);
        //获取文件路径
        String absolutePath = minio.getAbsolutePath();

        return iMediaFilesService.upLoadFile(comId, mediaParamsDto, absolutePath, objectname);
    }
}
